package com.guli.cms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.cms.entity.CrmBanner;
import com.guli.cms.service.CrmBannerService;
import com.guli.utils.R;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author ZhaoZhenyu
 * @since 2022-10-01
 */

@Api(description = "banner管理")
//@CrossOrigin
@RestController
@RequestMapping("/cms/banner")
public class CrmBannerController {

    @Autowired
    private CrmBannerService crmBannerService;

    // 查询全部
    @GetMapping("/getAllBanner")
    public R getAllBanner(){
        // 改成redis
        List<CrmBanner> crmBannerList = crmBannerService.getAllBanner();
        return R.ok().data("items", crmBannerList);
    }

    // 分页查询
    @GetMapping("/getBannerByPage/{current}/{size}")
    public R getBannerByPage(@PathVariable Integer current,@PathVariable Integer size){
        Page<CrmBanner> pageParam = new Page<>(current,size);
        crmBannerService.page(pageParam, null);
        List<CrmBanner> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok().data("items", records).data("total", total);
    }

    @GetMapping("/get/{id}")
    public R get(@PathVariable String id){
        CrmBanner crmBanner = crmBannerService.getById(id);
        return R.ok().data("item", crmBanner);
    }


    @PostMapping("/save")
    public R save(@RequestBody CrmBanner banner) {
        crmBannerService.save(banner);
        return R.ok();
    }

    @PutMapping("/update")
    public R updateById(@RequestBody CrmBanner banner) {
        crmBannerService.updateById(banner);
        return R.ok();
    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable String id) {
        crmBannerService.removeById(id);
        return R.ok();
    }


}

